package com.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PYUtil {
	/**
	 * 
	 * 方法简介:获取每个汉字的拼音
	 * <p>方法详述</p>
	 * @param chineseString 中文字符串
	 * @param full 是否需要全拼 true:全拼    false：首字母拼音
	 * @author 张龙
	 * @throws BadHanyuPinyinOutputFormatCombination 
	 * @date 创建时间 2015-3-16
	 * @since V1.0
	 */
	public static String getFullPY(String chineseString, boolean full) throws BadHanyuPinyinOutputFormatCombination{
		String[] temp;
		ArrayList<String[]> list = new ArrayList<String[]>();
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] input = chineseString.trim().toCharArray();
        for (int i = 0; i < input.length; i++) {
        	if (Character.toString(input[i]).matches("[\u4E00-\u9FA5]+")) {
        		//可能会有多个拼音，如“长”可以是chang也可以是zhang
        		temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
        		if(full == false){//不是全拼
        			for(int j=0;j<temp.length;j++){
        				temp[j] = temp[j].substring(0, 1);
        			}
        		}
        		temp = removeTheSame(temp);//去除读音里面一样的部分
        		list.add(temp);
        	}else{
        		list.add(new String[]{Character.toString(input[i])});
        	}
        }
        dKEj(list);
		return dKEj(list);
	}
	/**
	 * 
	 * 方法简介:去除字符数组中重复的字符串
	 * <p>方法详述</p>
	 *
	 * @author 张龙
	 * @date 创建时间 2015-3-16
	 * @since V1.0
	 */
	private static String[] removeTheSame(String[] para){
		Map map = new HashMap<String, String>();
		StringBuffer sqlBF = new StringBuffer();
		for(int i=0;i<para.length;i++){
			map.put(para[i], "");
		}
		
		Iterator<String> iterator = map.keySet().iterator();
		while(iterator.hasNext()){
			sqlBF.append(iterator.next()+",");
		}
		
		return sqlBF.toString().split(",");
	}
	/**
	 * 
	 * 方法简介:获取笛卡尔积
	 * <p>方法详述</p>
	 *
	 * @author 张龙
	 * @date 创建时间 2015-3-16
	 * @since V1.0
	 */
	private static String dKEj(ArrayList<String[]> list){
		StringBuffer sqlBF = new StringBuffer();
		String[] temp, first, second, newArray;
		int lengthF, lengthS, total;
		if(list==null){
			return null;
		}
		
		if(list.size()==1){//如果只有一个就直接拼串返回
			temp = list.get(0);
			for(int i =0;i<temp.length;i++){
				sqlBF.append(temp[i]+",");
			}
			sqlBF.substring(0, sqlBF.length()-1);
		}else{//不止一个的话就需要计算了
			first = (String[])list.get(0);
			second = (String[])list.get(1);
			lengthF = first.length;
			lengthS = second.length;
			total = lengthF*lengthS;
			newArray = new String[total];
			for(int i=0;i<lengthF;i++){
				for(int j=0;j<lengthS;j++){
					total = total -1;
					newArray[total] = first[i]+second[j];
				}
			}
			list.set(0, newArray);
			list.remove(1);
			sqlBF.append(dKEj(list));
		}
		return sqlBF.substring(0, sqlBF.length()-1);
	}
}
